package Controller;

import java.util.Date;
import java.util.List;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.RenderRequest;
import javax.xml.rpc.ServiceException;

import models.OutputJob;
import models.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.portlet.ModelAndView;

import util.AddandSort;
import Service.DateFormatter;
import Service.GetServerInfo;
import Service.LaunchJobHelper;
import Service.UserService;



@Controller
@SuppressWarnings("unchecked")
@RequestMapping(value = "VIEW")
public class DashboardController extends BaseController {

	@Autowired
	private UserService userService;
	
	private String selectedURL = "";
	private String selectedName ="";
	private GetServerInfo sinfo=new GetServerInfo();
	private String cmd = "";
	private String outURL="";
	private String basic_cmd="";
	private String appid = "";
	

	
	//get service list controller
	@RequestMapping(params = "action=servicelist")
	public ModelAndView srlView(RenderRequest request) throws Exception {
		ModelAndView mv = new ModelAndView();
		userService.setSrvList();
		List<service> services = userService.getSrvList();
		AddandSort as = new AddandSort();
		
		mv.addObject("servicesF", services.subList(0, services.size()/3));
		mv.addObject("servicesS", services.subList(services.size()/3, (services.size()/3)*2 ));
		mv.addObject("servicesT", services.subList((services.size()/3)*2, services.size() ));
		mv.addObject("services", as.doSortandAdd(services));
		mv.setViewName("serviceList");
		return mv;
	}
	
	//reset service list -- refresh
	@RequestMapping(params = "action=refresh")
	public void refreshSRV(ActionRequest request, ActionResponse response){
		userService.resetlist();
		response.setRenderParameter("action", "servicelist");
	}
	
	//show gui look of the service
	@RequestMapping(params = "action=showGUI")
	public ModelAndView guiView(RenderRequest request) throws Exception {
		ModelAndView mv = new ModelAndView();
		
			cmd="";//reset cmd
			basic_cmd = "";	
			
			//reset service info
			sinfo = new GetServerInfo();
			sinfo.Config(selectedURL);
		mv.addObject("appname", selectedName);
		mv.addObject("usage", sinfo.getUsage());
		mv.addObject("infos", sinfo.getInfos());	
		if(!sinfo.isBasic()){
			mv.addObject("groups",sinfo.getGroup_set() );
			mv.setViewName("application");
		}
		else{
			mv.setViewName("application-basic");			
		}
		return mv;
	
	}

	//show job result
	@RequestMapping(params = "action=showResult")
	public ModelAndView resltView(RenderRequest request) throws Exception {
		ModelAndView mv = new ModelAndView();
		String time = DateFormatter.format(new Date());
		userService.addjobs(outURL,cmd,selectedName,time,appid,selectedURL);
		mv.addObject("appname", selectedName);
		mv.addObject("appurl",selectedURL);
		mv.addObject("cmd", cmd);
		mv.addObject("appid", appid);
		mv.addObject("outURL", outURL);
		mv.addObject("subtime",time);
		mv.setViewName("jobResult");
		return mv;
	
	}
	
	
	//show gui metadata action
	@RequestMapping(params = "action=showMeta")
	public void showGUI(ActionRequest request, ActionResponse response){
		selectedURL = request.getParameter("serviceURL");
		selectedName = request.getParameter("serviceName");
		response.setRenderParameter("action", "showGUI");
	}
	
	//get input after submit
	@RequestMapping(params = "action=getInput")
	public void collectInput(ActionRequest request, ActionResponse response) throws ServiceException{
		
		//if user click submit button
		if(request.getParameter("check").equals("Submit")){
			
			// call LaunchJobHelper to launch job	
			LaunchJobHelper helper = new LaunchJobHelper();
			helper.doLaunch(selectedURL, request, sinfo);
			
			cmd = helper.getCmd();//cmd to run the application
			if(!helper.getOutURL().equals("Error during Submission")){
				outURL = "http://ws.nbcr.net/"+helper.getOutURL(); //output url.
			}
			else{
				outURL=helper.getOutURL();
			}
            appid =helper.getOutURL();
            
			response.setRenderParameter("action", "showResult");  

			
		}
		//click on reset, redirect to this page again
		else if(request.getParameter("check").equals("Reset") ){
			response.setRenderParameter("action", "showGUI"); 
		}
		//click on cancel, redirect to home page/servicelist page
		else if(request.getParameter("check").equals("Cancel")){
			response.setRenderParameter("action", "servicelist"); 
		}
	}
	
	//get job output
	//show job result
	@RequestMapping(params = "action=getJoblist")
	public ModelAndView jobView(RenderRequest request) throws Exception {
		ModelAndView mv = new ModelAndView();
		List<OutputJob> jobs = userService.getJobs();
		mv.addObject("jobs", jobs);
		mv.setViewName("joburl");
		return mv;
	}
		

}